/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.nyquistmeteocal.beans;

import it.nyquistmeteocal.entity.CalendarEvents;
import it.nyquistmeteocal.boundary.CalendarEventsManager;
import it.nyquistmeteocal.entity.Notification;
import it.nyquistmeteocal.boundary.NotificationManager;
import it.nyquistmeteocal.boundary.UserManager;
import it.nyquistmeteocal.entity.UserNotification;
import it.nyquistmeteocal.boundary.UserNotificationManager;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

/**
 *
 * @author gatb27
 */
@Named(value = "invites")
@RequestScoped
public class ListInviteBean implements Serializable {

    private static final long serialVersionUID = 1L;
    
    @EJB
    private NotificationManager nm;
    @EJB
    private UserManager um;
    @EJB
    private CalendarEventsManager cm;
    @EJB
    private UserNotificationManager unm;
    
    private CalendarEvents calev = new CalendarEvents();
    private UserNotification usernotification = new UserNotification();
    
    private String LogUser;
    private Integer unid;
    
    private List<Notification> inviteList;

    public List<Notification> getInviteList() {
        return inviteList;
    }

    public void setInviteList(List<Notification> inviteList) {
        this.inviteList = inviteList;
    }
    
    @PostConstruct
    public void init() {
        
            inviteList = nm.getInvites();
        
    }    
    
    public String AcceptInvite(Integer eventid, Integer notid) {
        LogUser=um.getLoggedUser().getEmail();
        System.out.println("the invite ID is: " + eventid);
        calev.setEventId(eventid);
        calev.setUserEmail(LogUser);
        cm.saveCalendarEvents(calev);
        /* we must delete the entry in usernotification, not in notification
        notification.setId(notid);
        notification.setEventid(eventid);
        notification.setText(text);
        notification.setType(type);
        nm.deleteNotification(notification);*/
        unid=nm.findUserNotification(notid);
        usernotification.setId(unid);
        System.out.println("XXXX the id is: " + usernotification.getId());
        usernotification.setNotificationid(notid);
        System.out.println("XXXXX the notificationid is: " + usernotification.getNotificationid());
        usernotification.setUsermail(LogUser);
        System.out.println("XXXXX the usermail is: " + usernotification.getUsermail());
        
        unm.deleteUserNotification(usernotification);
        
        
        
        return "showConfirm";
    }
    
    public String DeclineInvite(Integer notid) {
        unid=nm.findUserNotification(notid);
        usernotification.setId(unid);
        usernotification.setNotificationid(notid);
        usernotification.setUsermail(LogUser);
        
        unm.deleteUserNotification(usernotification);
        
        return "showConfirm2";
    }
        
}
    
    
    

